package net.jiastudy.biz.datasync.strategy.gxcm;

import net.jiastudy.biz.datasync.SyncUtil;
import net.jiastudy.biz.datasync.strategy.gxcm.bean.OrgBean;
import net.jiastudy.biz.datasync.strategy.gxcm.bean.TeacherBean;
import net.jiastudy.biz.enums.DeptType;
import net.jiastudy.biz.enums.Gender;
import net.jiastudy.biz.sys.entity.Dept;
import net.jiastudy.biz.sys.service.DeptService;
import net.jiastudy.biz.user.entity.User;
import net.jiastudy.biz.user.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

@Component
public class GxcmAsyncTaskService {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private UserService userService;
    @Resource
    private DeptService deptService;

    public Future<String> createTeachers(List<TeacherBean> teacherBeans, Map<String, OrgBean> orgMap) {
        for (TeacherBean teacherBean : teacherBeans) {
            if (null != teacherBean.getXm() && null != teacherBean.getZgh() && teacherBean.getSzdwdm() != null && !teacherBean.getSzdwdm().isEmpty()) {
                User dbUser = userService.getUserByUsername(teacherBean.getZgh());

                Dept dbDept = deptService.getDeptByCode(teacherBean.getSzdwdm());
                Long deptId;
                if (null == dbDept) {
                    Dept dept = new Dept();
                    dept.setName(orgMap.get(teacherBean.getSzdwdm()).getMc());
                    dept.setType(DeptType.other);
                    dept.setCode(teacherBean.getSzdwdm());
                    dept = deptService.createDept(dept);
                    deptId = dept.getId();
                } else {
                    deptId = dbDept.getId();
                }

                if (null == dbUser) {
                    User user = new User();
                    user.setDeptId(deptId);

                    user.setName(teacherBean.getXm());
                    user.setUsername(teacherBean.getZgh());

                    user.setPassword(teacherBean.getZgh());
                    if (null != teacherBean.getXbdm() && teacherBean.getXbdm() == "1") {
                        user.setGender(Gender.male);
                    } else if (null != teacherBean.getXbdm() && teacherBean.getXbdm() == "2") {
                        user.setGender(Gender.female);
                    }
                    userService.createUser(user);
                } else {
                    dbUser.setDeptId(deptId);
                    userService.updateUser(dbUser);
                }
            }
        }
        SyncUtil.decrTeacherSyncBatchCount();
        return new AsyncResult<>("批量创建教师成功");
    }
}
